package com.ssm.controller;

import com.alibaba.fastjson.JSONObject;


import com.ssm.entity.Teacher;
import com.ssm.entity.UmAdmin;
import com.ssm.entity.UmStudent;
import com.ssm.entity.UmTeacher;
import com.ssm.service.AdminService;
import com.ssm.service.StudentService;
import com.ssm.service.TeacherService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class ChagePasswordController extends BaseController {
    @Resource(name = "studentService")
    private StudentService studentService;
    @Resource(name = "adminService")
    private AdminService adminService;
    @Resource(name = "teacherService")
    private TeacherService teacherService;


    @RequestMapping(value = "chagepasswordpage",method = RequestMethod.GET)
    public String page(HttpSession session){

        return "page";
    }

        //修改密码
        @ResponseBody
        @RequestMapping(value = "chagepassword",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
        public String checkLogin(HttpSession session, @RequestParam String lastpassword, @RequestParam String newpassword){
            int IsChage=0;
            Object o=session.getAttribute("adminId");
            if (o!=null){
                logger.info("权限验证成功，为管理员ID：{}",o);
                UmAdmin admin=new UmAdmin();
                admin=adminService.getUmAdmin(Integer.parseInt(o.toString()));
                if (admin.getPassword().equals(lastpassword)){

                    UmAdmin admin1=new UmAdmin();
                    admin1.setPassword(newpassword);
                    admin1.setAdminid(admin.getAdminid());
                    IsChage=adminService.update(admin1);
                    admin1=null;
                    admin=null;
                }
            }
            Object o1=session.getAttribute("studentId");
            if (o1!=null){
                logger.info("权限验证成功，为学生ID：{}",o1);
                UmStudent umStudent=new UmStudent();
                logger.info(o1);
                umStudent= studentService.getUmStudent(Integer.parseInt(o1.toString()));

                if (umStudent.getPassword().equals(lastpassword)){

                    logger.info("原密码正确");
                    UmStudent umStudent1=new UmStudent();
                    umStudent1.setPassword(newpassword);
                    umStudent1.setStudentid(umStudent.getStudentid());
                    IsChage= studentService.update(umStudent1);
                    umStudent1=null;
                    umStudent=null;
                }
            }
            Object o2=session.getAttribute("teacherId");
            if (o2!=null){
                logger.info("权限验证成功，为教师ID：{}",o2);
                UmTeacher teacher=new UmTeacher();
                teacher=teacherService.getUmTeacher(Integer.parseInt(o2.toString()));
                if(teacher.getPassword().equals(lastpassword)){

                    UmTeacher teacher1=new UmTeacher();
                    teacher1.setPassword(newpassword);
                    teacher1.setTeacherid(teacher.getTeacherid());
                    IsChage=teacherService.update(teacher1);
                    teacher1=null;
                    teacher=null;

                }
            }

            //Boolean IsChage=adminService.chagepassword(lastpassword,newpassword);

            JSONObject object=new JSONObject();
            if (IsChage==1){
                logger.info("密码修改成功");
                object.put("success",true);
            }else{
                logger.info("原密码错误");
                object.put("success",false);
            }
            return object.toJSONString();
        }
}
